<template>
  <el-dialog :title="title" :visible.sync="dialogShow" @close="closeDalog">
    <el-form
      :model="formData"
      :rules="formRules"
      ref="form"
      label-width="220px"
    >
      <el-form-item label="权限名称" prop="name">
        <el-input v-model="formData.name"></el-input>
      </el-form-item>
      <el-form-item label="权限标识" prop="code">
        <el-input v-model="formData.code"></el-input>
      </el-form-item>
      <el-form-item label="权限描述" prop="description">
        <el-input v-model="formData.description"></el-input>
      </el-form-item>
      <el-form-item label="是否可用" prop="enVisible">
        <!-- <el-input v-model="formData.enVisible"></el-input> -->
        <!-- 1显示 0隐藏 -->
        <el-switch
          v-model="formData.enVisible"
          active-value="1"
          inactive-value="0"
        ></el-switch>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" @click="btnOk">确定</el-button>
        <el-button size="small" @click="dialogShow = false">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import {
  addPermissiontApi,
  getPermissiontInfoApi,
  editPermissiontApi
} from '@/api/permission'
export default {
  name: 'AddPerm',
  data() {
    return {
      dialogShow: false,
      formData: {
        name: '',
        code: '',
        description: '',
        type: '', // 权限类型 1一级菜单权限，2子级权限
        pid: '', // 上级id是谁
        enVisible: '0'
      },
      formRules: {
        name: [{ required: true, message: '请输入', trigger: 'blur' }],
        code: [{ required: true, message: '请输入', trigger: 'blur' }],
        description: [{ required: true, message: '请输入', trigger: 'blur' }]
      }
    }
  },
  computed: {
    title() {
      return this.formData.id ? '修改' : '添加'
    }
  },
  methods: {
    async btnOk() {
      try {
        await this.$refs.form.validate()
      } catch (error) {
        return
      }
      if (this.formData.id) {
        await editPermissiontApi(this.formData)
        this.$message.success('修改成功')
      } else {
        await addPermissiontApi(this.formData)
        this.$message.success('添加成功')
      }

      this.$emit('updateList')
      this.dialogShow = false
    },
    closeDalog() {
      this.formData = {
        name: '',
        code: '',
        description: '',
        type: '', // 权限类型 1一级菜单权限，2子级权限
        pid: '', // 上级id是谁
        enVisible: '0'
      }
      this.$refs.form.clearValidate()
    },
    async getRowInfo(id) {
      const res = await getPermissiontInfoApi(id)
      console.log(res)
      this.formData = res
      this.dialogShow = true
    }
  }
}
</script>

<style></style>
